#include <set>
#include <map>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>

using namespace std;

const int W = 16;
const int H = 7;

const char* str[] = {
"111111MMM11111111MMMMMMMMMMM111111111MMMMMMMM1111MMMMMMMMMMM11111MMMMMMMMMMMM1111MMMMMMMMMMMMM11",
"11111MM1MM1111111MM11111111MM111111MM1111111MM111MM111111111MM111MM11111111111111MM1111111111111",
"1111MM111MM111111MM11111111MM11111MM111111111MM11MM1111111111MM11MM11111111111111MM1111111111111",
"111MMMMMMMMM11111MMMMMMMMMMM111111MM1111111111111MM1111111111MM11MMMMMMMMMMMM1111MMMMMMMMMMMMM11",
"11MM1111111MM1111MM11111111MM11111MM111111111MM11MM1111111111MM11MM11111111111111MM1111111111111",
"1MMM11111111MM111MM11111111MM111111MM1111111MM111MM111111111MM111MM11111111111111MM1111111111111",
"1MM1111111111MM11MMMMMMMMMMM111111111MMMMMMMM1111MMMMMMMMMMM11111MMMMMMMMMMMM1111MM1111111111111",
"11111MMMMMMMM1111MM111111111MM1111111MMMMMM111111111MMMMMMMM111111MM111111MMM11111MM111111111111",
"111MM1111111MM111MM111111111MM111111111MM11111111111111MM111111111MM11111MMM111111MM111111111111",
"11MM111111111MM11MM111111111MM111111111MM11111111111111MM111111111MM111MMM11111111MM111111111111",
"11MM1111111111111MMMMMMMMMMMMM111111111MM11111111111111MM111111111MMMMM11111111111MM111111111111",
"11MM111111MMMMM11MM111111111MM111111111MM1111111111MM11MM111111111MM111MMM11111111MM111111111111",
"111MM1111111MM111MM111111111MM111111111MM1111111111MMM1MM111111111MM11111MMM111111MM111111111111",
"11111MMMMMMMMM111MM111111111MM1111111MMMMMM1111111111MMMM111111111MM111111MMMM1111MMMMMMMMMMMM11",
"1MM1111111111MM11MMM111111111MM111111MMMMMM111111MMMMMMMMMMM111111111MMMMMM111111MMMMMMMMMMM1111",
"1MMMM111111MMMM11MMMM11111111MM1111MMM1111MMM1111MM111111111MM11111MMM1111MMM1111MM111111111MM11",
"1MM1MM1111MM1MM11MM1MM1111111MM111MMM111111MMM111MM1111111111MM111MMM111111MMM111MM1111111111MM1",
"1MM11MMMMM111MM11MM11MM111111MM11MM1111111111MM11MM111111111MM111MM1111111111MM11MM111111111MM11",
"1MM1111M11111MM11MM1111MM1111MM111MMM111111MMM111MMMMMMMMMMM111111MMM1MMMM1MMM111MMMMMMMMMMM1111",
"1MM1111111111MM11MM111111MMM1MM1111MMM1111MMM1111MM1111111111111111MMM11MMMMM1111MM11111111MM111",
"1MM1111111111MM11MM11111111MMMM111111MMMMMM111111MM1111111111111111111MMMM1MMMM11MM111111111MMM1",
"1111MMMMMMMM111111MMMMMMMMMMMM111MM1111111111MM11MMMM111111MMMM11MM1111111111MM111MMM111111MMM11",
"111MM1111111MM1111MMMMMMMMMMMM111MM1111111111MM111MMM111111MMM111MM1111111111MM1111MMM1111MMM111",
"11MMM1111111MMM11111111MM11111111MM1111111111MM111MMM111111MMM1111MM111MM111MM111111MMM11MMM1111",
"1111MMMMM11111111111111MM11111111MM1111111111MM1111MMM1111MMM11111MM111MM111MM11111111MMMM111111",
"1MMM111MMMM111111111111MM11111111MMM11111111MMM11111MMM11MMM111111MM111MM111MM111111MMM11MMM1111",
"111MMM11111MMM111111111MM11111111MMM11111111MMM111111MM11MM1111111MM1MM11MM1MM11111MMM1111MMM111",
"11111MMMMMMM11111111111MM1111111111MMMMMMMMMM111111111MMMM111111111MMM1111MMM11111MMM111111MMM11",
"11MMM111111MMM11111MMMMMMMMMM111",
"111MMM1111MMM1111111111111MM1111",
"1111MMM11MMM1111111111111MM11111",
"111111MMMM11111111111111MM111111",
"1111111MM1111111111111MM11111111",
"1111111MM111111111111MM111111111",
"1111111MM1111111111MMMMMMMMMMM11",
""};

typedef pair<int, int> PII;
typedef vector<PII> VPII;
map<VPII, char> mp;

void uniform(VPII& v) {
    int x = v[0].first, y = v[0].second;
    for (int i = 1; i < (int)v.size(); ++i) {
        x = min(x, v[i].first);
        y = min(y, v[i].second);
    }
    for (int i = 0; i < (int)v.size(); ++i) {
        v[i].first -= x;
        v[i].second -= y;
    }
    sort(v.begin(), v.end());
}

void init() {
    VPII pat, tap;
    for (int k = 0; k < 26; ++k) {
        int x = k / 6 * H, y = k % 6 * W;
        pat.clear();
        tap.clear();
        for (int i = 0; i < H; ++i) {
            for (int j = 0; j < W; ++j) {
                if (str[x + i][y + j] == 'M') {
                    pat.push_back(make_pair(i, j));
                    tap.push_back(make_pair(-i, -j));
                }
            }
        }
        uniform(pat);
        uniform(tap);
        mp[pat] = 'A' + k;
        mp[tap] = 'A' + k;
    }
}

const int MAXN = 512;
char buf[MAXN][MAXN];

void floodfill(int x, int y, VPII& v) {
    buf[x][y] = 'W';
    v.push_back(make_pair(x, y));
    for (int i = x - 1; i <= x + 1; ++i) {
        for (int j = y - 1; j <= y + 1; ++j) {
            if (buf[i][j] == 'M') {
                floodfill(i, j, v);
            }
        }
    }
}

int main() {
    int n, m;
    VPII v;
    set<char> ans;

    init();
    while (scanf("%d%d", &n, &m) != EOF) {
        for (int i = 1; i <= n; ++i) {
            scanf("%s", buf[i] + 1);
        }
        memset(buf[n + 1], 0, sizeof(buf[n + 1]));
        ans.clear();
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (buf[i][j] == 'M') {
                    v.clear();
                    floodfill(i, j, v);
                    uniform(v);
                    ans.insert(mp[v]);
                }
            }
        }
        for (set<char>::iterator it = ans.begin(); it != ans.end(); ++it) {
            putchar(*it);
        }
        puts("");
    }

    return 0;
}

//Run ID    Submit Time     Judge Status    Problem ID      Language    Run Time(ms)    Run Memory(KB)      User Name
//2157650   2010-04-12 22:43:19     Accepted    3314    C++     0   432     watashi@Zodiac

// 2012-09-07 02:07:02 | Accepted | 3314 | C++ | 0 | 436 | watashi | Source
